package edu.uwosh.cs342.group1.database;
import java.io.*;
import java.math.BigInteger;
import java.security.*;
import java.security.spec.*;
import java.util.ArrayList;

import javax.crypto.*;
import javax.crypto.spec.*;

public class StringEncryptor {
	public static String Encrypt(String input1) {
		
        String the_string = input1;
        String password = "knockers";

    
        try {
            InputStream in = new ByteArrayInputStream(the_string.getBytes() );

            // Create a key.
            byte[] desKeyData = password.getBytes();
            DESKeySpec desKeySpec = new DESKeySpec(desKeyData);
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
            SecretKey desKey = keyFactory.generateSecret(desKeySpec);

            // Use Data Encryption Standard cipher
            Cipher des = Cipher.getInstance("des");
            des.init(Cipher.ENCRYPT_MODE, desKey);
      
            ArrayList<Byte> enc_bytes = new ArrayList<Byte>();
      
            byte[] input = new byte[64];
            while (true) {
                int bytesRead = in.read(input);
                if (bytesRead == -1) break;
                byte[] output = des.update(input, 0, bytesRead);
                if (output != null) 
                    for (int i = 0; i < output.length; i++) enc_bytes.add(new Byte(output[i]));  
            }
      
            byte[] output = des.doFinal();
            if (output != null) 
                for (int i = 0; i < output.length; i++) enc_bytes.add(new Byte(output[i]));  
            in.close();
            Object enc_data_wrap [] =  enc_bytes.toArray();
            byte enc_data [] = new byte [enc_data_wrap.length];
            for (int i = 0; i < enc_data_wrap.length; i++) 
                enc_data[i] = ((Byte)enc_data_wrap[i]).byteValue();
            BigInteger bi = new BigInteger(enc_data);
            input1= bi.toString();
            //fout.close();
        }
        catch (InvalidKeySpecException e) {System.err.println(e);}
        catch (InvalidKeyException e) {System.err.println(e);}
        catch (NoSuchAlgorithmException e) {
            System.err.println(e);
            e.printStackTrace();
        }
        catch (NoSuchPaddingException e) {System.err.println(e);}
        catch (BadPaddingException e) {System.err.println(e);}
        catch (IllegalBlockSizeException e) {System.err.println(e);}
        catch (IOException e) {System.err.println(e);}
        System.out.println(input1);
        return input1;
	}
	
	public static String Decrypt(String input) {
		 String password = "knockers";
		 
	        try {

	            BigInteger bi = new BigInteger(input);
	            InputStream bin = new ByteArrayInputStream(bi.toByteArray() );

	            // Create a key.
	            byte[] desKeyData = password.getBytes();
	            DESKeySpec desKeySpec = new DESKeySpec(desKeyData);
	            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
	            SecretKey desKey = keyFactory.generateSecret(desKeySpec);

	            // Use Data Encryption Standard cipher
	            Cipher des = Cipher.getInstance("des");
	            des.init(Cipher.DECRYPT_MODE, desKey);

	            String decrypted_string = new String("");

	            byte[] input1 = new byte[64];
	            while (true) {
	                int bytesRead = bin.read(input1);
	                if (bytesRead == -1) break;
	                byte[] output = des.update(input1, 0, bytesRead);
	                if (output != null) decrypted_string = decrypted_string.concat(new String(output)) ;
	            }
	      
	            byte[] output = des.doFinal();
	            if (output != null) decrypted_string = decrypted_string.concat(new String(output));
	            bin.close();
	            System.out.println(decrypted_string);
	            input = decrypted_string;
	        }
	        catch (InvalidKeySpecException e) {System.err.println(e);}
	        catch (InvalidKeyException e) {System.err.println(e);}
	        catch (NoSuchAlgorithmException e) {
	            System.err.println(e);
	            e.printStackTrace();
	        }
	        catch (NoSuchPaddingException e) {System.err.println(e);}
	        catch (BadPaddingException e) {System.err.println(e);}
	        catch (IllegalBlockSizeException e) {System.err.println(e); e.printStackTrace();}
	        catch (IOException e) {System.err.println(e);}
	        
			return input;
	}
	
	
	// key might be hard-coded, which then they can be removed
}
